/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.createdtmassmethodgen;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.operation.Determination;
import com.inspur.edp.bef.core.determination.builtinimpls.BefOnCancelDtmAssembler;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefAftModifyPredicateDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefOnCancelDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.predicate.ExecutePredicateFactory;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.entityimpl.BefEntityImplGenCtx;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.operation.CommonDetermination;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createdtmassmethod.EntityCreateDtmAssMethodGenerator;
import com.inspur.edp.cef.generator.overall.GenUtils;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import com.inspur.edp.common.component.actionextension.CancelAttachmentTempDeterAdaptor;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Statement;

public class CreateOnCancelDtmAssMethodGenerator extends EntityCreateDtmAssMethodGenerator {

  private BefEntityImplGenCtx entityCtx;

  public CreateOnCancelDtmAssMethodGenerator(BefEntityImplGenCtx entityCtx, String nodeCode) {
    super(entityCtx, nodeCode);
    this.entityCtx = entityCtx;
  }

  @Override
  protected List<CommonDetermination> getDtmMediateTypes() {
    return entityCtx.getDataType().getDtmCancel();
  }

  @Override
  protected TypeInfo getDtmAssClassType() {
    addImport(BefOnCancelDtmAssembler.class.getTypeName());
    return new TypeInfo(BefOnCancelDtmAssembler.class);
  }

  @Override
  protected TypeInfo getNewAdaptor_TypeParam(CommonDetermination op) {
    return CreateDtmAssMethodUtil.anyChildRequestElement(op)
        ? new TypeInfo(BefOnCancelDtmAdaptor.class)
        : new TypeInfo(BefAftModifyPredicateDtmAdaptor.class);
  }

  @Override
  protected String getMethodName() {
    return "addOnCancelDtm";
  }

  @Override
  public boolean skip() {
    return super.skip() && buildAutoAddingStatements().isEmpty();
  }

  @Override
  protected void buildExtendNewAdaptorParameter(CommonDetermination val, List args) {
    if(CreateDtmAssMethodUtil.anyChildRequestElement(val)) {
      GspBizEntityObject dataType = (GspBizEntityObject) getEntityImplGenContext().getDataType();
      args.add(Utils.buildChgTypes(ast, val.getGetExecutingDataStatus()));
      if (val.getRequestElements().isEmpty() && val.getRequestChildElements().isEmpty()) {
        return;
      }
      args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(),
          val.getRequestElements()));
      args.add(Utils.buildRequestChildElements(ast, dataType, val.getRequestChildElements()));
    } else {
      addImport(ExecutePredicateFactory.class.getTypeName());
      args.add(CreateDtmAssMethodUtil.buildPredicate(ast, val, getEntityImplGenContext()));
      args.add(ast.newBooleanLiteral(false));
    }
  }

  @Override
  protected void addExtendingStatements(Block block) {
    block.statements().addAll(buildAutoAddingStatements());
  }

  private List<Statement> autoAddings;
  private List<Statement> buildAutoAddingStatements() {
    if(autoAddings == null) {
      autoAddings = new ArrayList<>();
      IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
      for (IGspCommonField element : obj.getContainElements()) {
        if (element.getIsUdt() && element.getUdtID().equals("ee86e96e-6d51-48c7-9e92-9b7dfe3b43b6")) {
          addNewCancelAttachement(autoAddings);
          break;
        }
      }
    }
    return autoAddings;
  }

  private void addNewCancelAttachement(List<Statement> sts) {
    addImport(new TypeInfo(CancelAttachmentTempDeterAdaptor.class));
    ClassInstanceCreation adaptorInstanceCreation = GenUtils
        .buildClassInstantiation(ast, CancelAttachmentTempDeterAdaptor.class);
    MethodInvocation methodInvocation = RepositoryGenUtil
        .createMethodInvocation(ast, ast.newSimpleName(varNameAssembler), "addDetermination", null);
    methodInvocation.arguments().add(adaptorInstanceCreation);
    sts.add(ast.newExpressionStatement(methodInvocation));
  }
}
